home *** CD-ROM | disk | FTP | other *** search
/ Best of Shareware / Best of PC Windows Shareware 1.0 - Wayzata Technology (7111) (1993).iso / mac / DOS / CAD_CAM / A7221V1B / SEND7221.C < prev    next >
C/C++ Source or Header  |  1992-03-16  |  11KB  |  322 lines

  1. /*
  2.    Module: send7221.c
  3.    Date:   3/9/92
  4.    Version: 1.0b
  5.    Author:  Dave Lutz
  6.    Email:   lutz@psych.rochester.edu
  7.    Copyright: 1992 University of Rochester, Psychology Dept.
  8.  
  9.    Disclaimer:  This software is distributed free of charge.  As such, it
  10.                 comes with ABSOLUTELY NO WARRANTY.  The user of the software
  11.                 assumes ALL RISKS associated with its use.
  12.  
  13.                 Your rights to modify and/or distribute this software are
  14.                 outlined in the file SEND7221.DOC.
  15.  
  16.    Purpose: This module provides a method to send hp7221 codes to an hp7221
  17.             plotter for plotting.  It reads the codes from a file, and sends
  18.             them out one of the serial ports.  The program handles buffer
  19.             control to avoid buffer overflow on large jobs.
  20. */
  21.  
  22. #include <time.h>
  23. #include <stdio.h>
  24. #include "input.h"
  25. #include "comio.h"
  26. #include "hpcodes.h"
  27. #include "retcodes.h"
  28.  
  29.  
  30. /* local funtion prototypes */
  31. void usage(void);
  32. int sendbuff (PLTFILE *infile, COMPORT *com);
  33.  
  34.  
  35. int main (argc, argv)
  36.    int argc;
  37.    char *argv[];
  38. {
  39.    int i, readcnt;
  40.    char *infilename;
  41.    int baudrate;
  42.    unsigned commask, baudmask, datamask, stopmask, paritymask, initmask;
  43.    time_t timeout;
  44.    PLTFILE *infile = (PLTFILE *) NULL;
  45.    COMPORT com;
  46.  
  47.    /* print welcome message */
  48.    printf ("SEND7221 1.0b.  University of Rochester, Psychology Dept.\n\n");
  49.  
  50.    /* set defaults */
  51.    commask    = COM1;
  52.    baudmask   = BAUD_2400;
  53.    datamask   = DATA_7;
  54.    stopmask   = STOP_1;
  55.    paritymask = PAR_E;
  56.    timeout    = 120;  /* seconds */
  57.    infilename = (char *)NULL;
  58.  
  59.    /* process command line args */
  60.    i = 0;
  61.    while (++i < argc) {
  62.       if (argv[i][0] == '/') {
  63.          if (argv[i][2] != ':') {
  64.             fprintf (stderr, "Error: Invalid option: %s\n",argv[i]);
  65.             usage();
  66.             exit (BADARGS);
  67.          } else {
  68.             switch (argv[i][1]) {
  69.                case 'C':
  70.                case 'c':
  71.                   /* set com port */
  72.                   switch (argv[i][3]) {
  73.                      case '1':
  74.                         commask = COM1;
  75.                         break;
  76.                      case '2':
  77.                         commask = COM2;
  78.                         break;
  79.                      case '3':
  80.                         commask = COM3;
  81.                         break;
  82.                      case '4':
  83.                         commask = COM4;
  84.                         break;
  85.                      default:
  86.                         fprintf (stderr,"Error: Invalid com port: %s\n",
  87.                                  argv[i]);
  88.                         usage();
  89.                         exit (BADARGS);
  90.                   }
  91.                   break;
  92.                case 'B':
  93.                case 'b':
  94.                   /* set baud rate */
  95.                   baudrate = atoi(&argv[i][3]);
  96.                   switch (baudrate) {
  97.                      case 300:
  98.                         baudmask = BAUD_300;
  99.                         break;
  100.                      case 1200:
  101.                         baudmask = BAUD_1200;
  102.                         break;
  103.                      case 2400:
  104.                         baudmask = BAUD_2400;
  105.                         break;
  106.                      default:
  107.                         fprintf (stderr, "Error: Invalid baud: %s\n",
  108.                                  argv[i]);
  109.                         usage();
  110.                         exit (BADARGS);
  111.                   }
  112.                   break;
  113.                case 'D':
  114.                case 'd':
  115.                   /* set data bits */
  116.                   switch (argv[i][3]) {
  117.                      case '7':
  118.                         datamask = DATA_7;
  119.                         break;
  120.                      case '8':
  121.                         datamask = DATA_8;
  122.                         break;
  123.                      case 'D':
  124.                      case 'd':
  125.                         datamask = DATA_D; /* don't care */
  126.                         break;
  127.                      default:
  128.                         fprintf (stderr, "Error: Invalid data bits: %s\n",
  129.                                  argv[i]);
  130.                         usage();
  131.                         exit (BADARGS);
  132.                   }
  133.                   break;
  134.                case 'S':
  135.                case 's':
  136.                   /* set stop bits */
  137.                   switch (argv[i][3]) {
  138.                      case '1':
  139.                         stopmask = STOP_1;
  140.                         break;
  141.                      case '2':
  142.                         stopmask = STOP_2;
  143.                         break;
  144.                      default:
  145.                         fprintf (stderr, "Error: Invalid stop bits: %s\n",
  146.                                  argv[i]);
  147.                         usage();
  148.                         exit (BADARGS);
  149.                   }
  150.                   break;
  151.                case 'P':
  152.                case 'p':
  153.                   /* set parity */
  154.                   switch (argv[i][3]) {
  155.                      case 'E':
  156.                      case 'e':
  157.                         paritymask = PAR_E;
  158.                         break;
  159.                      case 'O':
  160.                      case 'o':
  161.                         paritymask = PAR_O;
  162.                         break;
  163.                      case 'N':
  164.                      case 'n':
  165.                         paritymask = PAR_N;
  166.                         break;
  167.                      case 'D':
  168.                      case 'd':
  169.                         paritymask = PAR_D;  /* don't care */
  170.                         break;
  171.                      default:
  172.                         fprintf (stderr, "Error: Invalid parity: %s\n",
  173.                                  argv[i]);
  174.                         usage();
  175.                         exit (BADARGS);
  176.                   }
  177.                   break;
  178.                case 'T':
  179.                case 't':
  180.                   /* set timeout in seconds */
  181.                   timeout = (time_t) atoi(&argv[i][3]);
  182.                   break;
  183.                default:
  184.                   fprintf (stderr, "Error: Invalid option: %s\n", argv[i]);
  185.                   usage();
  186.                   exit (BADARGS);
  187.             }
  188.          } 
  189.       } else {
  190.          /* argv[i][0] != '/' */
  191.          if (infilename != (char *)NULL) {
  192.             fprintf (stderr, "Error: Too many input files specified.\n");
  193.             usage();
  194.             exit (BADARGS);
  195.          } else {
  196.             infilename = argv[i];
  197.          }
  198.       }
  199.    }
  200.    if (infilename == (char *) NULL) {
  201.       fprintf (stderr, "Error: Input file not specified.\n");
  202.       usage();
  203.       exit (BADARGS);
  204.    }
  205.  
  206.    switch (openin(infilename, &infile)) {
  207.       case TRUE:
  208.          break;
  209.       case NOBUFF:
  210.          fprintf (stderr, "Error: can't allocate input buffer.\n");
  211.          exit (NOBUFF);
  212.       case BADOPEN:
  213.          fprintf (stderr, "Error: can't open input file for reading.\n");
  214.          exit (BADOPEN);
  215.    }
  216.    
  217.    initmask = (datamask | stopmask | paritymask | baudmask);
  218.    (void) cominit (&com, commask, initmask, timeout);
  219.  
  220.    if (comout (&com, HP_INIT, HP_INIT_BYTES) != HP_INIT_BYTES) {
  221.       fprintf (stderr, "Error: I/O error sending init string to plotter.\n");
  222.       (void) closein (&infile);
  223.       exit (BADCIO);
  224.    }
  225.  
  226.    while ((readcnt = fillbuff(infile)) > 0) {
  227.       if (!sendbuff (infile, &com)) {
  228.          fprintf (stderr, "Error: I/O error sending data to plotter.\n");
  229.          (void) closein (&infile);
  230.          exit (BADCIO);
  231.       }
  232.    }
  233.    if (readcnt == -1) {
  234.        fprintf (stderr, "Error: I/O error reading input file.\n");
  235.        (void) comout (&com, HP_ABORT, HP_ABORT_BYTES);
  236.        (void) closein (&infile);
  237.        exit (BADFIO);
  238.    }
  239.    if (!closein (&infile)) {
  240.       fprintf (stderr, "Error: I/O error closing input file.\n");
  241.       exit (BADFIO);
  242.    }
  243.    if (comout (&com, HP_END, HP_END_BYTES) != HP_END_BYTES) {
  244.       fprintf (stderr, "Error: I/O error sending HP_END command to plotter.\n");
  245.       exit (BADCIO);
  246.    }
  247.    exit (ALLOK);
  248. }
  249.  
  250.  
  251.  
  252. /*
  253.    Function: usage
  254.    Purpose:  Print out a usage message for the program.
  255.  
  256.    Pre: none
  257.  
  258.    Post:  A usage message is printed to stderr.
  259. */
  260.  
  261. void usage(void)
  262. {
  263.    fprintf (stderr, "Usage: SEND7221 [/C:?] [/D:?] [/S:?] [/P:?] [/B:????] infile\n\n");
  264.    fprintf (stderr, "\t/C:? = Com port number (1-4)        Default=1\n");
  265.    fprintf (stderr, "\t/D:? = Number of data bits (7/8/D)  Default=7\n");
  266.    fprintf (stderr, "\t/S:? = Number of stop bits (1/2)    Default=1\n");
  267.    fprintf (stderr, "\t/P:? = Parity (E/O/N/D)             Default=E\n");
  268.    fprintf (stderr, "\t/B:???? = Baud rate (300/1200/2400) Default=2400\n");
  269.    fprintf (stderr, "\t/T:???? = read Timeout in seconds   Default=120\n");
  270. }
  271.  
  272.  
  273. /*
  274.    Function: sendbuff
  275.    Purpose:  Send the contents of the infile buffer to the HP7221.
  276.  
  277.    Pre: infile is a pointer to a PLTFILE that has been opened for reading.
  278.         The buffer in infile has been filled by a call to fillbuff.
  279.         comp is a pointer to a COMPORT.
  280.         comp has been set up by a call to cominit.
  281.  
  282.    Post: An attempt is made to send the contents of the buffer to the HP7221.
  283.          If all of the bytes in the buffer can't be sent, FALSE is returned.
  284.          Otherwise, TRUE is returned.
  285. */
  286.  
  287. int sendbuff (infile, comp)
  288.    PLTFILE *infile;
  289.    COMPORT *comp;
  290. {
  291.    char cts,  /* storage for clear_to_send character */
  292.         *buf_ptr;
  293.  
  294.    int fullblocks, /* number of HP_BLOCKSIZE blocks needed to send the data */
  295.        lbsize;     /* size of last (short) block, if needed */
  296.  
  297.    fullblocks = infile->buf_ctr / HP_BLOCKSIZE;
  298.    lbsize     = infile->buf_ctr % HP_BLOCKSIZE;
  299.  
  300.    buf_ptr = infile->buf;
  301.  
  302.    while (fullblocks-- > 0) {
  303.       if (comout (comp, HP_RTS, HP_RTS_BYTES) != HP_RTS_BYTES)
  304.          return (FALSE);
  305.       if (comin (comp, &cts, 1, -1) < 1)
  306.          return (FALSE);
  307.       if (comout (comp, buf_ptr, HP_BLOCKSIZE) != HP_BLOCKSIZE)
  308.          return (FALSE);
  309.       buf_ptr += HP_BLOCKSIZE;
  310.    }
  311.    if (lbsize != 0) {
  312.       if (comout (comp, HP_RTS, HP_RTS_BYTES) != HP_RTS_BYTES)
  313.          return (FALSE);
  314.       if (comin (comp, &cts, 1, -1) < 1)
  315.          return (FALSE);
  316.       if (comout (comp, buf_ptr, lbsize) != lbsize)
  317.          return (FALSE);
  318.       buf_ptr += lbsize;
  319.    }
  320.    return (TRUE);
  321. }
  322.